package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.pojo.TbSpecGroup;
import com.leyou.item.mapper.TbSpecGroupMapper;
import com.leyou.item.service.TbSpecGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 规格参数的分组表，每个商品分类下有多个规格参数组 服务实现类
 * </p>
 *
 * @author leyou
 * @since 2020-02-29
 */
@Service
public class TbSpecGroupServiceImpl extends ServiceImpl<TbSpecGroupMapper, TbSpecGroup> implements TbSpecGroupService {

    @Autowired
    private TbSpecGroupMapper tbSpecGroupMapper;

    /**
     * 规格参数->一级分类->二级分类->三级分类->对应组
     * 根据 分类id查询 对应的 规格参数组
     * @param cateoryId
     * @return
     */
    @Override
    public List<SpecGroupDTO> findSpecGroupByCategoryId(Long cateoryId) {
        // 不需要分页查询，直接拼接查询条件
        QueryWrapper<TbSpecGroup> queryWrapper = new QueryWrapper<>();
        // 判断  cateoryId  groupId
        queryWrapper.lambda().eq(TbSpecGroup::getCid, cateoryId);
        // 执行查询
        List<TbSpecGroup> tbSpecGroups = this.getBaseMapper().selectList(queryWrapper);
        // 非空判断
        if (CollectionUtils.isEmpty(tbSpecGroups)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        // 转换成 SpecGroupDTO
        return BeanHelper.copyWithCollection(tbSpecGroups, SpecGroupDTO.class);
    }
}
